﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration.Install;
using Microsoft.Win32;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;

namespace VTFC
{
    // ----------------------------------------------------------------------------
    /// <summary>
    /// Custom installer logic to associate Content.tfc and VTFC application.
    /// </summary>
    // ----------------------------------------------------------------------------
     [RunInstaller(true)]
    public class CustomInstaller : Installer
    {
        // ----------------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        // ----------------------------------------------------------------------------
        public CustomInstaller()
        {
        }//ctor 

        // ----------------------------------------------------------------------------
        /// <summary>
        /// After install, set extension association.
        /// </summary>
        /// <param name="savedState"></param>
        // ----------------------------------------------------------------------------
        protected override void OnAfterInstall(IDictionary savedState)
        {
            base.OnAfterInstall(savedState);
            Associate(Context.Parameters["targetdir"]);
        }//func OnAfterInstall

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Associate application with .tfc extension.
        /// </summary>
        /// <param name="targetdir">Setup folder.</param>
        // ----------------------------------------------------------------------------
        public void Associate(string targetdir)
        {
            string extension = Path.GetExtension(Constant.VTFC_CONTENT_FILE);

            //create key for .tfc extension
            if (Registry.ClassesRoot.OpenSubKey(extension, false) != null)
            {
                string progID = GetAssemblyToken();
                string description = Constant.VTFC_APPLICATION_SMALL_NAME;
                string icon = Path.Combine(targetdir, "App.ico");
                string application = Path.Combine(targetdir, "VTFC.exe");
                                
                Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);

                using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
                {
                    key.SetValue("", description);
                    key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));
                    key.CreateSubKey(@"Shell\Open\Command").SetValue("", ToShortPathName(application) + " \"%1\"");
                }
            }
        }//func Associate

        [DllImport("Kernel32.dll")]
        private static extern uint GetShortPathName(string lpszLongPath, [Out] StringBuilder lpszShortPath, uint cchBuffer);//func GetShortPathName
                
        private string ToShortPathName(string longName)
        {
            StringBuilder sb = new StringBuilder(255);
            uint iSize = (uint)sb.Capacity;
            uint iRet = GetShortPathName(longName, sb, iSize);
            return sb.ToString();
        }//func ToShortPathName
         
        private string GetAssemblyToken() 
        {
            Assembly assembly = Assembly.GetAssembly(typeof(VTFC.CustomInstaller));
            object[] obj = assembly.GetCustomAttributes(typeof(GuidAttribute), false);
            if (obj.Length > 0)
            {
                return ((GuidAttribute)obj[0]).Value;
            }
            else
            {
                return Guid.NewGuid().ToString();
            }
        }//func GetAssemblyToken
    }//class CustomInstaller
}//namespace VTFC
